package com.hzqy.web.ac.itf;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hzqy.commons.utils.SystemConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.service.ift.YxtServiceBean;
import com.hzqy.service.wailian.WaiLianServiceBean;
import com.hzqy.web.vo.WlContentVo;

@Controller
@RequestMapping("/itf")
public class YxtAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());

	@Resource
	private YxtServiceBean yxtServiceBean;
	
	@Resource
	private WaiLianServiceBean waiLianServiceBean ;

	@RequestMapping(value = "/bindAccount", method = RequestMethod.POST)
	@ResponseBody
	public Object bindAccount(String serverid, String account, String appkey, String stat, String msg) {
		String url =  SystemConfigUtils.getSysConfig("yxt_url1");
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_path(url);
		wlContentVo.setSysConfigKey("control_link_ab");
		wlContentVo = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		Map<String, Object> result = new HashMap<>();
		
		if(wlContentVo!=null){
			//t1作为回调接口的日志开始和结束的匹配标识, 并且计算接口处理用时。
			long t1 = System.currentTimeMillis();
			Map<String, Object> resultMap = new HashMap<String, Object>();
			try {
				LOG.debug("(" + t1 + ") 翼校通对" + serverid + "注入类型: " + stat + "的account帐号 :" + account + " msg=" + msg);
				resultMap = yxtServiceBean.updateServerAccount(serverid, account, appkey, stat, msg);
			} catch (Exception e) {
				LOG.error("", e);
			}
			LOG.debug("(" + t1 + ") 业务账号: " + serverid + " 翼校通账号: " + account + " 注入结果: " + resultMap.toString() + "回调耗时: " + (System.currentTimeMillis() - t1) + "ms");
			return resultMap;
		}else{
			result.put("msg", "接口已关闭");
			result.put("success", "0");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/getContentStatus")
	public Object getContentStatus(
			HttpServletRequest request) {
		long t1 = System.currentTimeMillis();
		String id = request.getParameter("id");
		LOG.debug("(" + t1 + ") 获取内容当前状态: id=" + id);
		Map<String, Object> result = yxtServiceBean.getContentStatus(id);
		LOG.debug("(" + t1 + ") 获取内容当前状态: " + result + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
		return result;
	}
	
	
}
